package com.example.zhaojianxu.signin.signIn.server

import android.annotation.SuppressLint
import android.app.Service
import android.content.Context
import android.content.Intent
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.location.LocationProvider
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import com.example.zhaojianxu.signin.signIn.contsance.Constance

/**
 * 定位服务
 */
class LocationService: Service(){

    private lateinit var locationManager: LocationManager

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onUnbind(intent: Intent?): Boolean {
        return super.onUnbind(intent)
    }

    @SuppressLint("MissingPermission")
    override fun onCreate() {
        super.onCreate()
        locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1.0f, listener)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    object listener : LocationListener {
        override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
            when (status) {
                LocationProvider.AVAILABLE -> {
                    Log.d(Constance.TAG, "onStatusChanged  当前GPS状态为可见")
                }
                LocationProvider.OUT_OF_SERVICE -> {
                    Log.d(Constance.TAG, "onStatusChanged  当前GPS状态为在服务区外")
                }
                LocationProvider.TEMPORARILY_UNAVAILABLE -> {
                    Log.d(Constance.TAG, "onStatusChanged  当前GPS状态为暂停服务")
                }
            }
        }

        override fun onProviderEnabled(provider: String?) {
            Log.d(Constance.TAG, "onProviderEnabled GPS开启")
        }

        override fun onProviderDisabled(provider: String?) {
            Log.d(Constance.TAG, "onProviderDisabled GPS禁用")
        }

        override fun onLocationChanged(location: Location?) {
            Constance.MY_LOCATION = location
            Log.d(Constance.TAG, "时间：" + location!!.time)
            Log.d(Constance.TAG, "经度：" + location!!.longitude)
            Log.d(Constance.TAG, "纬度：" + location!!.latitude)
            Log.d(Constance.TAG, "海拔：" + location!!.altitude)
        }
    }
}